<?php
$objXajax->registerFunction("fmfrmreportdetail_load");
$objXajax->registerFunction("fmfrmreportdetail_deleteDetail");

$objXajax->registerFunction("fmfrmreportdetail_setPhonenumber");
$objXajax->registerFunction("fmfrmreportdetail_saveSMS");

function fmfrmreportdetail_setPhonenumber(){
      global $lang,$objDbUpdate,$hotel_id;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $sql = 'select phone_auto_send_sms as phone from cus_config where service_id = '.$hotel_id;    
    $arrPhone = $objDbUpdate->GetArray($sql);
    $phone = $arrPhone[0]['phone'];     
    $objResponse->addScript("$('#fmfrmreportdetail_txtphonenumber').val('$phone'); "); 
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
}

function fmfrmreportdetail_load($dateFrom,$dateTo,$reporttype=1){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrsum = array();
    $arrData = fmfrmreportdetail_createArray($arrsum,$dateFrom,$dateTo,$reporttype);
    $count = count($arrData);
     
    $oTbs = new clsTinyButStrong();
    
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_reportdetail_grid'.$reporttype.'.html');
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->MergeBlock('blk_data_sum', $arrsum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('fmfrmreportdetail_divGrid', 'innerHTML', $oTbs->Source);
    if($reporttype==1){
     $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('fmfrmreportdetail_tbl',true);");    
    }else{
      $objResponse->addScriptCall("dataTableObj.CreatDataTable('fmfrmreportdetail_tbl',true);");   
    }
        
    $dateFrom = ConvertDate($dateFrom,true);
    $now =  date('Y-m-d H:i:s'); 
    if($now>$dateTo){
      $dateTo = ConvertDate($dateTo,true);
      $dateTo = str_replace('/','.',$dateTo);   
    }else{
      $dateTo = date('d.m.Y H:i:s');  
    }
    $dateFrom = str_replace('/','.',$dateFrom);
    $total = FormatMoney($arrsum[0]['total']);
    $smscontent = SMS_SYSTEM_MONEY_PRODUCT.$total. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_FROM.$dateFrom. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_TO.$dateTo; 
    $objResponse->addScript("fmfrmreportdetail_smscontent='$smscontent';");  
    $objResponse->addScript("$('#fmfrmreportdetail_doanhthusms').html('$total'); ");
    $objResponse->addScript("$('#fmfrmreportdetail_timeFrom').html('$dateFrom'); "); 
    $objResponse->addScript("$('#fmfrmreportdetail_timeTo').html('$dateTo'); ");
         
    $objResponse->addScript('changeWaiting(1);');                     
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function fmfrmreportdetail_createArray(&$arrSum,$dateFrom,$dateTo,$reporttype=1){
    global $objDbUpdate,$tbl_postfix,$hotel_id;

    $sql='';
    switch($reporttype){                                                                                                                   
        case 1:  $sql = "  SELECT sum(c.num*(c.price+c.area_extprice)*(100-promotion)/100) as total, c.product_id,c.date_update ,f.name ,rt.name as roomtype
        FROM cus_history_product$tbl_postfix c  left join fm_room f on c.table_id = f.id  left join fm_room_type rt on f.room_type = rt.id 
      where c.flg=1  and c.date_update>'$dateFrom' and c.date_update<'$dateTo'  
      group by c.table_id order by rt.name,f.name ;"; 
        break;
        case 2: $sql = " SELECT sum(num) as num ,sum(c.num*(c.price+c.area_extprice)*(100-promotion)/100) as total, c.product_id,c.date_update ,c.name
        FROM cus_history_product$tbl_postfix c  left join cus_product$tbl_postfix f on c.product_id = f.id
      where c.flg=1  and c.date_update>'$dateFrom' and c.date_update<'$dateTo'  
      group by c.product_id order by c.name "; 
    break;      
        case 3: $sql = " SELECT sum(c.num*(c.price+c.area_extprice)*(100-promotion)/100) as total,c.date_update ,f.username 
        FROM cus_history_product$tbl_postfix c  left join tbl_user f on c.user_update = f.id
      where c.flg=1  and c.date_update>'$dateFrom' and c.date_update<'$dateTo'  
      group by f.username order by f.username  "; 
    break;
        case 4:  $sql = "  SELECT LPAD(c.general_id,7,0) as mhd,sum(c.num*(c.price+c.area_extprice)*(100-promotion)/100) as total, c.product_id,c.date_update ,f.name,c.date_update 
        FROM cus_history_product$tbl_postfix c  left join fm_room f on c.table_id = f.id
        
      where c.flg=1  and c.date_update>'$dateFrom' and c.date_update<'$dateTo'  
      group by c.general_id order by  c.general_id ;"; 
        break;
    
    }
    //echo $sql; die();
    $arr = array();
    try{
        $arr = $objDbUpdate->GetArray($sql);   
        $count =  count($arr);
        $total = 0;
        for($i=0;$i<$count;$i++){
            $arr[$i]['total']= round($arr[$i]['total'],-3);
           $total +=$arr[$i]['total'];             
        }
        $arrSum[0]['total']= $total;
        
    }
    catch(Exception $e){
        $arr = array();
    } 
    return  $arr  ;
}

function fmfrmreportdetail_deleteDetail($dateDelete){
    global $objDbUpdate,$tbl_postfix,$hotel_id;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax      
    if($_SESSION['current']['power_type_id']==POWER_CUSTOMER){
        $dateDelete = addslashes($dateDelete); 
        $sql = "delete from cus_history_product$tbl_postfix where datediff(date_update,'$dateDelete')<0 and datediff(date_update,now())<0 ";
        if($objDbUpdate->Execute($sql)){
            $objResponse->addAlert('Xóa dữ liệu thành công'); 
          $objResponse->addScript('fmfrmreportdetail_createReport();');   
        }else{
           $objResponse->addAlert('không xóa được dữ liệu'); 
        }    
        
    }else{
        $objResponse->addAlert('Bạn không đủ quyền để xóa');  
    }

    $objResponse->addScript('changeWaiting(1);'); 
     return $objResponse->getXML(); // dong luon luon co khi goi ham ajax                       
}

function fmfrmreportdetail_saveSMS($phonenumber,$fmfrmreportdetail_smscontent){
     global $objDbSelect,$user_id,$hotel_id;
     $objResponse = new xajaxResponse(); 
     $arrPhone = explode(';',$phonenumber);
     $num_phone = count($arrPhone);
     $sql = "INSERT INTO tbl_sms(date_created, sms_type, sms_content, service_id, phone_number, num_phone, user_created, sms_timesend)
     values(now(),1,'$fmfrmreportdetail_smscontent',$hotel_id,'$phonenumber',$num_phone,$user_id,now())" ;
     if($objDbSelect->Execute($sql)){
       $objResponse->addScript("$('#fmfrmreportdetail_divsms').dialog('close');");     
       $objResponse->addAlert("Bạn đã gửi tin nhắn thành công");     
     }     
    
     return $objResponse->getXML(); 
} 

# end region process data
?>
